package com.himalayan.bear.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * springmvc登陆拦截器
 *
 * @ClassName: LoginInterceptor
 * @Description: TODO
 * @author: dp
 * @date: 2016年8月23日 上午9:10:56
 */
@Slf4j
public class LoginInterceptor extends HandlerInterceptorAdapter {

    public LoginInterceptor() {
        super();
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        log.info("postHandle方法: handler: " + handler + ", modelAndView: " + modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        log.info("afterCompletion方法: handler: " + handler + ", modelAndView: " + ex);
    }

    @Override
    public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("afterConcurrentHandlingStarted方法: handler: " + handler);
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        HttpSession session = request.getSession();
        Object o = session.getAttribute("currentUser");
        if (o == null) {
            response.sendRedirect("toLogin");
            return false;
        }
        return true;
    }

}
